#include "CreateCubeWidget.h"

#include "Core/MainWindow.h"
#include "FeatureShape/FeatureShape.h"

CreateCubeWidget::CreateCubeWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateSolidWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreateCubeWidgetClass())
{
	ui->setupUi(this);
	bIsConfirmed = false;

	connect(ui->confirm, &QPushButton::clicked, this, &CreateCubeWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreateCubeWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreateCubeWidget::onApply);

	connect(ui->cubeLen, &QDoubleSpinBox::valueChanged, this, &CreateCubeWidget::onSizeChanged);
	connect(ui->cubeWidth, &QDoubleSpinBox::valueChanged, this, &CreateCubeWidget::onSizeChanged);
	connect(ui->cubeHeight, &QDoubleSpinBox::valueChanged, this, &CreateCubeWidget::onSizeChanged);

	connect(ui->pX, &QDoubleSpinBox::valueChanged, this, &CreateCubeWidget::onPosChanged);
	connect(ui->pY, &QDoubleSpinBox::valueChanged, this, &CreateCubeWidget::onPosChanged);
	connect(ui->pZ, &QDoubleSpinBox::valueChanged, this, &CreateCubeWidget::onPosChanged);

	connect(ui->selectCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CreateCubeWidget::OnComboBoxIndexChanged);
	m_originPoint = new RWTripleFloatInput(ui->pX, ui->pY, ui->pZ);

	// operation -> param
	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeCube* CubeShape = static_cast<FeatureShapeCube*>(CreateOP->m_featureShape.get());
		ui->cubeLen->setValue(CubeShape->_length);
		ui->cubeWidth->setValue(CubeShape->_width);
		ui->cubeHeight->setValue(CubeShape->_height);
		m_originPoint->SetValue(CubeShape->_position);
	}

	cacheActor = m_mainWindow->createCube(ui->cubeLen->value(), ui->cubeWidth->value(), ui->cubeHeight->value());
	onPosChanged(0.f);
}

CreateCubeWidget::~CreateCubeWidget()
{
	delete ui;
}

void CreateCubeWidget::onSizeChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	// 这里可能不是value真的发生了变化
	cacheActor = m_mainWindow->UpdateCubeActor(cacheActor, ui->cubeLen->value(), ui->cubeWidth->value(), ui->cubeHeight->value());
	onPosChanged(0.f);
}

void CreateCubeWidget::onPosChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	cacheActor = m_mainWindow->UpdateActorPosition(cacheActor, gp_Pnt(ui->pX->value(), ui->pY->value(), ui->pZ->value()));
}

ShapeOperation* CreateCubeWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("立方体"), cacheActor->m_featureShape);
	return op;
}

void CreateCubeWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeCube(cacheActor->OccShape(), ui->cubeLen->value(), ui->cubeWidth->value(), ui->cubeHeight->value()));
}

void CreateCubeWidget::Reset()
{
	QRWCreateSolidWidget::Reset();

	// 数据归零
	m_originPoint->SetValue(gp_Pnt());
	ui->cubeLen->setValue(1);
	ui->cubeWidth->setValue(1);
	ui->cubeHeight->setValue(1);

	cacheActor = m_mainWindow->createCube(ui->cubeLen->value(), ui->cubeWidth->value(), ui->cubeHeight->value());
	onPosChanged(0.f);
}
